<template>
    <div >
        <van-nav-bar
                :fixed="fixed"
                title="新增地址"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft"
        />
        <div :style="{marginTop:'46px'}">
            <van-address-edit
                    :area-list="areaList"
                    show-postal
                    show-set-default
                    show-search-result
                    :search-result="searchResult"
                    @save="onSave"
                    @delete="onDelete"
            />
        </div>
    </div>
</template>

<script>
    import areaList from './areaList'
    import Vue from 'vue'
    import { AddressEdit,NavBar,Toast} from 'vant';
    import {UserAddress} from "../api/data";
    Vue.use(AddressEdit,NavBar);
  export default {
      data() {
          return {
              fixed:true,
              areaList,
              searchResult: []
          }
      },
      methods: {
          onSave(content) {
              console.log(content)
              let name = content.name
              let tel = content.tel
              let province = content.province
              let county = content.city
              let city = content.county
              let address = content.addressDetail
              let postalcode = content.postalCode
              let code = (content.isDefault)?1:0
              let area_code = content.areaCode
              UserAddress(name,tel,province,county,city,address,postalcode,code,area_code).then(res=>{
                  if (res.data.code == '10001') {
                      Toast('登录已失效，请重新登录！')
                      this.$router.push({path:`/login`})
                      localStorage.removeItem('token')
                      return
                  }
                  Toast('添加成功！')
                  this.$router.back()
              })
          },
          onDelete() {
              Toast('delete');
          },
          onClickLeft() {
              this.$router.back()
          }
      }
  }
</script>

<style scoped>
    .title{
        border-bottom: 1px solid #BFBFC0;
    }
</style>
